/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-09-10
*/
package com.rzico.order.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.Enterprise;
import com.rzico.basics.service.EnterpriseService;
import com.rzico.entity.PageResult;
import com.rzico.entity.Pageable;
import com.rzico.order.entity.BargainBuy;
import com.rzico.order.entity.BargainOrder;
import com.rzico.order.service.BargainBuyService;
import com.rzico.order.service.BargainOrderService;
import com.rzico.order.service.BargainPeriodService;
import com.rzico.order.service.BargainProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 砍价查询接口
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-09-10
 */
@Api(description = "砍价查询接口")
@RestController
@RequestMapping("/bargainBuy")
public class BargainBuyController extends BaseController {

    @Autowired
    private BargainBuyService bargainBuyService;

    @Autowired
    private BargainProductService bargainProductService;

    @Autowired
    private BargainOrderService bargainOrderService;

    @Autowired
    private EnterpriseService enterpriseService;

    @Autowired
    private BargainPeriodService bargainPeriodService;


    /**
     * 分页查询团购管理
     *
     * @return
    */
    @ApiOperation("分页查询团购管理")
    @GetMapping("/list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mchId", value = "商户号", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "状态（0.待审核，1.已审核, 2.已完成,3.已取消）", dataType = "String", paramType = "query")
    })
    public CommResult<BargainBuy> list(String mchId, Integer status, Pageable pageable) {
        Map<String, Object> params = new HashMap<String, Object>();
        params = buildSortField(params, pageable);

//        if (status!=null){
            params.put("isShow", true);
//        }

        Enterprise enterprise = enterpriseService.findByMch(mchId);
        if (enterprise!=null) {
            params.put("enterpriseId", enterprise.getId());
        } else {
            return CommResult.error("无效商户号");
        }

        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<BargainBuy> list = bargainBuyService.selectList(params);
        for (BargainBuy bargainBuy:list) {
            bargainBuy.setPeriodList(bargainPeriodService.getItemList(bargainBuy.getId()));
        }
        PageResult<BargainBuy> pageResult = new PageResult<BargainBuy>(list, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);
    }

    /**
     * 查询单条团购管理
     *
     * @return
    */
    @ApiOperation("查询单条团购管理")
    @GetMapping("/find/{id}")
    public CommResult<BargainBuy> find(@PathVariable String id) {

        BargainBuy result = bargainBuyService.findById(id);

        result.setItemList(bargainProductService.getItemList(result.getId()));

        result.setPeriodList(bargainPeriodService.getItemList(result.getId()));

        Map<String,Object> data = new HashMap<>();
        data.put("bargainBuy",result);
        return CommResult.success(data);

    }


    /**
     * 查询历史订单
     *
     * @return
     */
    @ApiOperation("查询历史订单")
    @GetMapping("/getOrderList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "periodId", value = "期数ID", dataType = "String", paramType = "query"),
    })
    public CommResult<BargainOrder> getOrderList(Long periodId) {
        Map<String,Object> data = new HashMap<>();
        data.put("orderItem",bargainOrderService.getItemList(periodId));
        return CommResult.success(data);

    }

}
